﻿#include "UBindDevice.h"
#include "Global.h"
#include <qtimer.h>
UBindDevice::UBindDevice(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	initUi();
}

UBindDevice::~UBindDevice()
{
}

void UBindDevice::initUi()
{
	/**
	* @ Author: Gaojia
	* @ Description: 初始化设备树
	*/
	m_pModel = new QStandardItemModel(this);
	QStandardItem* rootItem = m_pModel->invisibleRootItem();
	ui.treeView->setModel(m_pModel);
	ui.treeView->header()->setVisible(false);
	ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu);  // ✅ 必须有
	buildItemTree(g_pRoot, rootItem);
	ui.treeView->expandAll();
	connect(ui.treeView, &QTreeView::clicked, this, &UBindDevice::onTreeClickeTemple);
	connect(ui.pushButton, &QPushButton::clicked, this, &UBindDevice::slot_clickedSave);
	connect(ui.pushButton_2, &QPushButton::clicked, this, &UBindDevice::slot_clickedCancel);
}

void UBindDevice::buildItemTree(CBaseNode* pNode, QStandardItem* pParentItem)
{
	if (!pNode) return;
	if (pNode->getType() == CBaseNode::enNode || pNode->getType() == CBaseNode::enRoot)
	{
		QStandardItem* pItem = new QStandardItem(pNode->getName());
		pItem->setData(QVariant::fromValue(reinterpret_cast<void*>(pNode)), Qt::UserRole);
		pParentItem->appendRow(pItem);
		for (CBaseNode* pChild : pNode->getChildren())
		{
			buildItemTree(pChild, pItem);
		}
	}
	else if (pNode->getType() == CBaseNode::enDevice)
	{
		CBaseDevice* pDevice = (CBaseDevice*)pNode;
	    QStandardItem* pItem = new QStandardItem(pNode->getName());
		pItem->setData(QVariant::fromValue(reinterpret_cast<void*>(pNode)), Qt::UserRole);
		pParentItem->appendRow(pItem);
		QModelIndex indexCurr = pItem->index(); // 获取该 item 对应的 QModelIndex

		if (indexCurr.isValid())
		{
			// 延迟调用，保证布局完成
			QTimer::singleShot(0, [this, indexCurr]() {
				ui.treeView->scrollTo(indexCurr, QAbstractItemView::PositionAtCenter);
				ui.treeView->setCurrentIndex(indexCurr);
				});
			ui.treeView->setCurrentIndex(indexCurr);   // 可选：高亮选中
		}
		ui.lineEdit->setText(pNode->getName());
		m_pCurrDevice = pDevice;
		
	}

}
void UBindDevice::slot_clickedSave()
{
	if (m_pCurrDevice != NULL)
	{
		QString sADDr = m_pCurrDevice->getCompletepath();
		QVector<QString>vecPointAddr;
		QVector<CBaseNode*>vecNode = m_pCurrDevice->getChildren();
		for (int i = 0; i < vecNode.size(); i++)
		{
			vecPointAddr.push_back(vecNode[i]->getCompletepath());
		}
		setProperty("DevAddr", QVariant::fromValue(m_pCurrDevice->getCompletepath()));
		setProperty("PointAddr", QVariant::fromValue(vecPointAddr));
		accept();  // 点击确定
	}
	else
	{
		showMessageBox(this, A2T("警告"), A2T("请正确选择设备！"), 1);
		return;
	}

}

void UBindDevice::slot_clickedCancel()
{
	close();
}

void UBindDevice::onTreeClickeTemple(const QModelIndex& index)
{
	if (!index.isValid()) return;
	QStandardItem* item = m_pModel->itemFromIndex(index);
	if (!item) return;
	CBaseNode* node = reinterpret_cast<CBaseNode*>(item->data(Qt::UserRole).value<void*>());
	if (!node) return;
	if (node->getType() == CBaseNode::enDevice)
	{
		m_pCurrDevice = (CBaseDevice*)node;
		ui.lineEdit->setText(m_pCurrDevice->getName());
	}
	else
	{
		m_pCurrDevice = nullptr;
		ui.lineEdit->setText("");
	}
}